<?php
namespace Views;
/**
 * File holds class for generating businesses view
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Views
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Business extends Page {
	private $_service;
	
	public function __construct($language) {
		$this->setUp($language, 0);
		$this->_service = new \Services\Business();
	}
	
	/**
	 * Method shows top businesses. Businesses are ordered by Net Promoter score
	 * @param int $type Type ID
	 */
	public function viewTopBusinesses($type = 0) {
		//read category businesses 
		$object = new \Entities\Business();
		$object->setType($type);
		$npsArray = array();
		$businesses = $this->_service->selectMulti($object);
		$feedbackService = new \Services\Feedback();
		$fb = new \Entities\Feedback();
		if (count($businesses) > 0 && $businesses) {
			foreach (array_keys($businesses) as $key) {
				//read ratings and generate NPS
				$fb->setBusiness($businesses[$key]->getId());
				$ratings = $feedbackService->selectMulti($fb);
				if ($ratings) {
					$nps = $this->_service->calculateNps($ratings);
				} else {
					$nps = 0;
				}
				$npsArray[$key] = $nps;
			}
		}
		$this->_smarty->assign('businessArray', $businesses);
		arsort($npsArray);
		$this->_smarty->assign('npsArray', $npsArray);
		$this->_content = $this->_smarty->fetch('topBusinesses.tpl');
		$this->display();
	}
	
	/**
	 * Method shows business details page
	 * @param int $id Business ID
	 */
	public function viewBusinessDetails($id) {
		$object = new \Entities\Business();
		$object->setId($id);
		$business = $this->_service->select($object);
		$feedbackService = new \Services\Feedback();
		$fb = new \Entities\Feedback();
		$fb->setBusiness($business->getId());
		$ratings = $feedbackService->selectMulti($fb);
		if ($ratings) {
			$nps = $this->_service->calculateNps($ratings);
		} else {
			$nps = 0;
		}
		$this->_smarty->assign('nps', $nps);
		$this->_smarty->assign('business', $business);
		$this->_smarty->assign('feedbacks', $ratings);
		$this->_content = $this->_smarty->fetch('companyDetails.tpl');
		$this->display();
	}
	
}